
app.config(function($routeProvider, $locationProvider) {
    $routeProvider.

		when('/card',{
			 templateUrl: 'card/list.html',
	         controller: 'cardListController'
		})
		. when('/card/index', {
            templateUrl: 'card/index.html',
            controller: 'cardIndexController'
        })
		. when('/card/create', {
            templateUrl: 'card/create.html',
            controller: 'cardCreateController'
        })
		 .when('/card/type', {
            templateUrl: 'card/type.html',
            controller: 'cardTypeController'
        })
        .when('/card/storeHistory', {
            templateUrl: 'card/storeHistory.html',
            controller: 'storeHistoryController'
        })
		 .when('/card/typeCreate/:code', {
            templateUrl: 'card/typeCreate.html',
            controller: 'cardTypeCreateController'
        })
		.when('/card/edit/:_id',{
			 templateUrl: 'card/edit.html',
	         controller: 'cardEditController'
		})
		.when('/card/setup/:_id',{
			 templateUrl: 'card/setup.html',
	         controller: 'cardEditController'
		})
		.when('/card/recharge/:_id',{
			 templateUrl: 'card/recharge.html',
	         controller: 'cardEditController'
		})
		.when('/card/consume/:_id',{
			 templateUrl: 'card/consume.html',
	         controller: 'cardEditController'
		})
		.when('/card/history/:card_id',{
			 templateUrl: 'card/record.html',
	         controller: 'cardHistoryController'
		})
		. when('/card/info/:transaction_no', {
            templateUrl: 'card/info.html',
            controller: 'cardInfoController'
        })
		. when('/card/info/:transaction_no', {
            templateUrl: 'card/info.html',
            controller: 'cardInfoController'
        });

});

function cardService( $http, $location ){

    return {

        bindMember : function( _id, app_id, person, recall ){
            var data = {
                name : person.name,
                phone:person.phone,
                cardid:person.cardid
            };
             baas_yl.call({
                'http': $http,
                'method': 'post',
                'uri': 'card/index/' + _id + '/bindElder',
                'data':data,
                'success': function(d) {
                   if (d.status == '1') {
	                    if (typeof recall == 'function') {
	                        recall(d);
	                    }
	                }else{
	                    if( d.description ){
	                        alert(d.description );
	                    }else{
	                        alert( '网络原因，请求失败，请稍后重试' );
	                    }
	                }
                }
            });
        }
        ,edit : function( _id, app_id, params, recall ){
            var data = { };
            if( typeof params.member != 'undefined' ){
                data.member = params.member;
            }
            if( typeof params.status != 'undefined' ){
                data.status = params.status;
            }
			
			baas_yl.call({
                'http': $http,
                'method': 'post',
                'uri': 'card/index/update/' + _id,
                'data':data,
                'success': function(d) {
                    if(d.status == '1'){
	                    if( typeof recall == 'function'){
	                        recall( d );
	                    }
	                }else{
	                    if( d.description ){
	                        alert(d.description );
	                    }else{
	                        alert( '网络原因，请求失败，请稍后重试' );
	                    }
	                }
                }
            });
        }
        ,find_one : function( _id, app_id, fn ){
            baas_yl.call({
                'http': $http,
                'method': 'get',
                'uri': 'card/index/elderid/' + _id,
                'success': function(d) {
                     if (typeof fn == 'function') {
	                    fn(d);
	                }
                }
            });
        }
        ,changeStatus : function( _id, app_id,status, fn ){
            var data = {
                status : status
            };
            baas_yl.call({
                'http': $http,
                'method': 'post',
                'uri': 'card/index/changeStatus/' + _id,
                'data' : data,
                'success': function(d) {
                     if (typeof fn == 'function') {
	                    fn(d);
	                }
                }
            });
        }
        ,add : function(manageId, app_id, data, fn ){
            var data = {
                number : data.number,
                fund   : data.fund,
                rule   : data.rule,
                 type   : data.type,
                 manageId :manageId
            };
			baas_yl.call({
                'http': $http,
                'method': 'post',
                'uri': 'card/index/create',
                'data' : data,
                'success': function(d) {
                    if(d.status == '1'){
	                    if( typeof fn == 'function'){
	                        fn( d );
	                    }
	                }else{
	                    if( d.description ){
	                        alert(d.description );
	                    }else{
	                        alert( '网络原因，请求失败，请稍后重试' );
	                    }
	                }
                }
            });
        }
        ,list : function(limit,page,app_id, fn ){     
//			var url = WEB_URL + '/card/index/list' ;
//        url += '?app_id=' + app_id+'&limit=20&page='+page;

            var param = {
                limit : limit,
                page : page
            };
//          $http.get( url ).success(function( d ){
//              if( typeof fn == 'function'){
//                  fn( d );
//              }
//          });

            baas_yl.call({
                'http': $http,
                'method': 'get',
                'uri': 'card/index/list',
                'data':param,
                'success': function(d) {
                    if( typeof fn == 'function'){
	                    fn( d );
	                }
                }
            });
        }
        ,searchCard : function( data,limit, page,app_id, fn ){
            var param = {
            	limit:limit,
            	page:page,
                card_id : (data.card_id != undefined)?data.card_id:'',
                cardid : (data.cardId != undefined)?data.cardId:'',
                name : (data.name != undefined)?data.name:'',
                phone : (data.phone != undefined)?data.phone:'',
                em_name : (data.em_name != undefined)?data.em_name:'',
                em_phone   : (data.em_phone != undefined)?data.em_phone:''
            };
            baas_yl.call({
                'http': $http,
                'method': 'post',
                'uri': 'card/index/searchCard',
                'data' : param,
                'success': function(d) {
                    if( typeof fn == 'function'){
	                    fn( d );
	                }
                }
            });
        }
        ,history : function( card_id,app_id, fn ){
            baas_yl.call({
                'http': $http,
                'method': 'get',
                'uri': 'card/index/history/'+ card_id,
                'success': function(d) {
                    if( typeof fn == 'function'){
	                    fn( d );
	                }
                }
            });
        }
        ,transaction : function( data, card_id, app_id, fn ){
            var data = {
                type : data.type,
                fund : data.fund,
                description : data.description,
                status :data.status,
                shop: typeof data.shop =='undefined'? "":data.shop
            };
            baas_yl.call({
                'http': $http,
                'method': 'post',
                'uri': 'card/index/transaction/' + card_id,
                'data' : data,
                'success': function(d) {
                    if( typeof fn == 'function'){
	                    fn( d );
	                }
                }
            });
        }
        ,information : function( transaction_no, app_id, fn ){
			baas_yl.call({
                'http': $http,
                'method': 'get',
                'uri': 'card/index/information/' + transaction_no,
                'success': function(d) {
                    if( typeof fn == 'function'){
	                    fn( d );
	                }
                }
            });
        }
        ,check_trans : function( type, transaction_id, app_id, fn ){
            if( type != 'charge' && type!= 'pay' ){
                alert('交易类型错误');
                return;
            }
            var data = {
                type : type
            };
            baas_yl.call({
                'http': $http,
                'method': 'post',
                'uri': 'card/index/'+ type + '/' + transaction_id,
                'data' : data,
                'success': function(d) {
                    if( typeof fn == 'function'){
	                    fn( d );
	                }
                }
            });
        }

		,typeList : function( app_id, fn ){
            baas_yl.call({
                'http': $http,
                'method': 'get',
                'uri': 'card/index/typeList',
                'success': function(d) {
                    if( typeof fn == 'function'){
	                    fn( d );
	                }
                }
            });
        }
        ,delType : function( data,app_id, fn ){
            var data = {
                name : data.name
            };
             baas_yl.call({
                'http': $http,
                'method': 'post',
                'uri': 'card/index/delType',
                'data' : data,
                'success': function(d) {
                    if( typeof fn == 'function'){
	                    fn( d );
	                }
                }
            });
        }
		
		,find_type : function( code,app_id, fn ){
             baas_yl.call({
                'http': $http,
                'method': 'get',
                'uri': 'card/index/findType/' + code,
                'success': function(d) {
                    if( typeof fn == 'function'){
	                    fn( d );
	                }
                }
            });
        }
		,createType : function( code,app_id, data, fn ){
        var data = {
            name : data.name,
            code : code
        };
		 baas_yl.call({
                'http': $http,
                'method': 'post',
                'uri': 'card/index/createType',
                'data' : data,
                'success': function(d) {
                    if(d.status == '1'){
		                if( typeof fn == 'function'){
		                    fn( d );
		                }
		            }else{
		                if( d.description ){
		                    alert(d.description );
		                }else{
		                    alert( '网络原因，请求失败，请稍后重试' );
		                }
		            }
                }
            });
    	}
		,searchArchives : function(member,app_id,fn){
			var params ={
				cardid: ( member.cardid !='' && member.cardid != undefined) ? member.cardid : undefined
			};
			baas_yl.call({
				'http' : $http,
				'method' : 'get',
				'uri' : 'archives/archives/list',
				'data' : params,
				'success' : function(d){
					if(typeof fn == 'function'){
						fn(d);
					}
				}
			});
		}
        ,cancelCard : function( card_id, app_id, fn ){
            baas_yl.call({
                'http': $http,
                'method': 'get',
                'uri': 'card/index/cancel/card/' + card_id,
                'success': function(d) {
                    if (typeof fn == 'function') {
                        fn(d);
                    }
                }
            });
        }
        ,renewCard : function( card_id, app_id, fn ){
            baas_yl.call({
                'http': $http,
                'method': 'get',
                'uri': 'card/index/renew/card/' + card_id,
                'success': function(d) {
                    if (typeof fn == 'function') {
                        fn(d);
                    }
                }
            });
        }
        //card/index/search/transaction/list
        ,transactionList : function(param,limit,page,app_id,fn){
            var data = {
                limit : typeof limit == "undefined"||limit==''?20:limit,
                page : typeof page == "undefined"||page==''?1:page,
                start_time : typeof param.start_time == "undefined"||param.start_time==''?'':param.start_time,
                end_time : typeof param.end_time == "undefined"||param.end_time==''?'':param.end_time,
                card_id : typeof param.card_number == "undefined"||param.card_number==''?'':param.card_number,
                type : 'pay',
                transaction_operator : typeof param.transaction_operator == "undefined"||param.transaction_operator==''?'':param.transaction_operator,
                member : typeof param.member == "undefined"||param.member==''?'':param.member,
                shop : typeof param.shop == "undefined"||param.shop==''?'':param.shop
            }
            baas_yl.call({
                'http': $http,
                'method': 'get',
                'data':data,
                'uri': 'card/index/search/transaction/list',
                'success': function(d) {
                    if (typeof fn == 'function') {
                        fn(d);
                    }
                }
            });
        }
        ,exportTransactionList : function(param,limit,page,app_id,fn){
            var data = {
                limit : typeof limit == "undefined"||limit==''?20:limit,
                page : typeof page == "undefined"||page==''?1:page,
                start_time : typeof param.start_time == "undefined"||param.start_time==''?'':param.start_time,
                end_time : typeof param.end_time == "undefined"||param.end_time==''?'':param.end_time,
                card_id : typeof param.card_number == "undefined"||param.card_number==''?'':param.card_number,
                transaction_operator : typeof param.transaction_operator == "undefined"||param.transaction_operator==''?'':param.transaction_operator,
                member : typeof param.member == "undefined"||param.member==''?'':param.member,
                type : 'pay',
                shop : typeof param.shop == "undefined"||param.shop==''?'':param.shop
            }
            baas_yl.call({
                'http': $http,
                'method': 'post',
                'data':data,
                'uri': 'card/index/export/transaction/list',
                'success': function(d) {
                    if (typeof fn == 'function') {
                        fn(d);
                    }
                }
            });
        }
    };
}

app.factory('cardService', ['$http','$location', cardService]);

app.controller('cardIndexController', ['$scope', '$routeParams', '$window', '$location', '$http', 'cardService', cardIndexController]);
app.controller('cardEditController', ['$scope', '$routeParams', '$window', '$location', '$http', '$timeout', 'cardService', cardEditController]);
app.controller('cardCreateController', ['$scope', '$routeParams', '$window', '$location', '$http', 'cardService', cardCreateController]);
app.controller('cardListController', ['$scope', '$routeParams', '$window', '$location', '$http', 'cardService', cardListController]);
app.controller('cardTypeController', ['$scope', '$routeParams', '$window', '$location', '$http', 'cardService', cardTypeController]);
app.controller('cardTypeCreateController', ['$scope', '$routeParams', '$window', '$location', '$http', 'cardService', cardTypeCreateController]);
//app.controller('cardsetupController', ['$scope', '$routeParams', '$window', '$location', '$http', 'cardService', cardsetupController]);
app.controller('cardHistoryController', ['$scope', '$routeParams', '$window', '$location', '$http', 'cardService', cardHistoryController]);
app.controller('cardInfoController', ['$scope', '$routeParams', '$window', '$location', '$http', 'cardService', cardInfoController]);
app.controller('storeHistoryController', ['$scope', '$routeParams', '$window', '$location', '$http', 'cardService', storeHistoryController]);

function storeHistoryController($scope, $routeParams, $window, $location, $http, cardService){

    $scope.app_id = $routeParams.app_id;
    $scope.selectedPage = 1;
    $scope.limit = 20;

    $scope.search = {};

    $scope.data = {
        selectedPage: $scope.selectedPage
    };


    $scope.getList = function (){
        $scope.params = {
            limit : 20,
            page  :  1,
            shop  : "到店消费"
        };
        cardService.transactionList($scope.params,$scope.limit,$scope.selectedPage,$scope.app_id,function( json ){
            $scope.transactionList = json['list'];
            $scope.count = json.count;
            $scope.currentPage = 1;
            $scope.getPage(json.count);
        });
    }
    $scope.getList();

    $scope.filter = function(){
        angular.element('#url').hide();
        angular.element('#range-ul').hide();
        $scope.export_button = false;
        $scope.search.start_time = angular.element("#start_time").val();
        $scope.search.end_time = angular.element("#end_time").val();
        $scope.search.shop="到店消费";
        cardService.transactionList($scope.search,$scope.limit,$scope.selectedPage,$scope.app_id,function( json ){
            $scope.transactionList = json['list'];
            $scope.count = json.count;
            $scope.currentPage = 1;
            $scope.getPage(json.count);

        });
    }

    $scope.changePage = function(p) {
        $scope.data.selectedPage = p;
        $scope.search.shop="到店消费";
        cardService.transactionList($scope.search,$scope.limit, p, $scope.app_id, function(json) {
            $scope.transactionList = json['list'];
            $scope.count = json.count;
            $scope.currentPage = p;
            $scope.getPage(json.count);
            console.log($scope.currentPage)
        });
    };
    $scope.getPage = function(count) {
        $scope.pageList = [];
        var limit = 20; //返回的条数
        $scope.totalPage = parseInt(count / limit); //总页数
        $scope.totalPage += count % limit >= 1 ? 1 : 0; //判断页数是否是整数
        var start = ($scope.currentPage - 2) >= 1 ? $scope.currentPage - 2 : 1; //首页
        var end = ($scope.currentPage + 2) <= $scope.totalPage ? $scope.currentPage + 2 : $scope.totalPage; //尾页

        $scope.frontdotShow = start > 1 ? true : false; //前面的省略号
        $scope.backdotShow = end < $scope.totalPage ? true : false; //后面的省略号
        for (var i = start; i <= end; i++) {
            $scope.pageList.push({
                number: i,
                active: i == $scope.currentPage ? 'active' : ''
            }); //给点击的按钮添加class
            console.log(count);
        }
    };


    $scope.getserviceType1 = function(code){
        baas_jz.call({
            'http': $http,
            'method': 'get',
            'uri': 'console/service/list/'+code,
            'success': function(d) {
                $scope.serviceList = d;
                console.log(d);
            }
        });
    };
    $scope.getserviceType1('0');

    $scope.getserviceType2 = function(code){
        baas_jz.call({
            'http': $http,
            'method': 'get',
            'uri': 'console/service/list/'+code,
            'success': function(d) {
                $scope.serviceList2 = d
            }
        });
    };
    //小类
    $scope.getserviceType3 = function(code){
        baas_jz.call({
            'http': $http,
            'method': 'get',
            'uri': 'console/service/list/'+code,
            'success': function(d) {
                $scope.serviceList3 = d
            }
        });
    };

    $scope.export = function(){
        $scope.export_button = true;
        $scope.search.start_time = angular.element("#start_time").val();
        $scope.search.end_time = angular.element("#end_time").val();
        $scope.search.shop="到店消费";
        cardService.exportTransactionList($scope.search,$scope.limit,$scope.selectedPage,$scope.app_id,function( d ){
            if(d.status){

                $scope.export_url = d.url;
                angular.element('#url').show();
                angular.element('#range-ul').show();
            }else{
                $scope.export_button = false;
                alert('网络错误，请重新导出');
            }
        });
    }
}

function cardEditController( $scope, $routeParams, $window, $location, $http, $timeout, cardService ){
    $scope.searchCardHistory = function(card_id){
        cardService.history(card_id , $scope.app_id,  function( json ){
            if(json.count == 0)
                $scope.renewTag = 0;
            else
                $scope.renewTag = 1;
        } );
    };
    $scope._id = $routeParams._id;
    $scope.app_id = $routeParams.app_id;

    $scope.find_one = function(  ){
        cardService.find_one( $scope._id, $scope.app_id,  function( json ){
            $scope.card = json;

            $scope.searchCardHistory($scope.card.card_id);
            if( typeof $scope.card.member != 'undefined' && $scope.card.member != '' ){
                $scope.card.bind = 1;
            }else{
                $scope.card.bind = 0;
            }
        } );
    };
    $scope.find_one();

    $scope.member = {};
    $scope.searchMember = function(){
        $timeout(function(){
            $http.get( url ).success(function( json ){
                $scope.member.findAll = json;
            });
            
            baas_yl.call({
                'http': $http,
                'method': 'get',
                'uri': 'card/elder/search/'+ $scope.member.search + '?app_id=' + $scope.app_id,
                'success': function(d) {
                  $scope.member.findAll = json;
                }
            });
        },200);
    };
    
    //根据身份证搜索老人信息
    $scope.searchArchives = function(){
    	cardService.searchArchives($scope.member, $scope.app_id,function(json){
    		$scope.member = json['list'][0];
    		console.log(json['list']);
    	});
    }

    $scope.dispatchCard = function( person ){
        //var member_id = person._id;
        cardService.bindMember( $scope._id, $scope.app_id, person, function( json ){
            $scope.find_one();
            alert("绑定老人信息成功！");
            var manageName = $scope.manager.manager_account 
             if( manageName =='admin'){
             	$location.path( '/card' );
             }else{
         	    $location.path( '/card/index' );
             }
//       $location.path( '/card/index' );
        });
    };
    //更改会员卡状态
    $scope.changeStatus = function(card){
        var status = card.status;
        if(status == -1){
            cardService.cancelCard( card.card_id, $scope.app_id, function( json ){
                $scope.find_one();
                alert("会员卡状态更新完成！");
                var manageName = $scope.manager.manager_account;
                if( manageName =='admin'){
                    $location.path( '/card' );
                }else{

                    $location.path( '/card/index' );
                }
            });
        }else if(status == 0){
            cardService.renewCard( card.card_id, $scope.app_id, function( json ){
                $scope.find_one();
                alert("会员卡状态更新完成！");
                var manageName = $scope.manager.manager_account;
                if( manageName =='admin'){
                    $location.path( '/card' );
                }else{

                    $location.path( '/card/index' );
                }
            });
        }
        else{
            cardService.changeStatus( $scope._id, $scope.app_id, status, function( json ){
                $scope.find_one();
                alert("会员卡状态更新完成！");
                var manageName = $scope.manager.manager_account;
                if( manageName =='admin'){
                    $location.path( '/card' );
                }else{

                    $location.path( '/card/index' );
                }
            });
        }
       
    };
    $scope.pay = function( card , type){

        var data = {
            type : type,
            fund : type == 'charge' ? card.charge_number : card.pay_number,
            description : card.description,
            status : card.status
        };
        if(card.chk){
            data.shop = "到店消费";
        }

        cardService.transaction( data, card.card_id, $scope.app_id, function( json ){

          //  console.log('交易产生');
            if( json.status ){
                cardService.check_trans( 'charge', json.transaction_no, $scope.app_id, function(){
                    alert('交易成功');
                    var manageName = $scope.manager.manager_account 
                    if( manageName =='admin'){
		                    $location.path( '/card' );
		                }else{
		                    $location.path( '/card/index' );
		                }
                } );
            }
            else{
                alert(json.description);
               }
        });

    };
}

function cardCreateController( $scope, $routeParams, $window, $location, $http, cardService ){
    $scope.app_id = $routeParams.app_id;
	$scope.getTypeList = function( ){
        cardService.typeList( $scope.app_id, function( json ){
            $scope.types = json.list;
        });
    };
    $scope.getTypeList();
    $scope.create = function( operation ){
        cardService.add( $scope.manageId,$scope.app_id, operation, function( json ){
            $location.path( '/card' );
        } );
    };
}

function cardListController( $scope, $routeParams, $window, $location, $http, cardService ){
//		var app_id = '5096fce5d7f8b112899f5bd7b2302bbe6e08d515';
//	    $scope.app_id = '5096fce5d7f8b112899f5bd7b2302bbe6e08d515';
   $scope.app_id = $routeParams.app_id;
    /*分页*/

    $scope.selectedPage = 1;
    $scope.data = {
        selectedPage  : $scope.selectedPage,
        page : $scope.page
    };
    $scope.limit = 20;
    $scope.getList = function( ){
        cardService.list( $scope.limit,$scope.selectedPage,$scope.app_id, function( json ){
            $scope.list = json.list;
           $scope.count =  json.count;
            $scope.currentPage = 1 ;
            $scope.getPage(json.count);
        });
    };
    $scope.searchCard = function(data ){
    	$scope.selectedPage = 1;
    	$scope.limit = 20;
        cardService.searchCard( data,$scope.limit, $scope.selectedPage,$scope.app_id, function( json ){
            $scope.list = json;
            $scope.count = json.count;
			$scope.currentPage = 1;
			$scope.getPage(json.count);
        });
    };
    $scope.getList();
    $scope.edit = function(){

    }
    
    /*分页*/
    $scope.changePage = function( p ){
        $scope.data.selectedPage = p;
        cardService.list($scope.limit,p,$scope.app_id, function( json ){ 
            $scope.list = json.list;
           $scope.count =  json.count;
            $scope.currentPage = p ;
            $scope.getPage(json.count);
        });
    };
   $scope.getPage = function(count){
        $scope.pageList = [];
        var limit = 20;//返回的条数
        $scope.totalPage = parseInt(count / limit);//总页数
        $scope.totalPage += count % limit >= 1 ? 1 : 0;//判断页数是否是整数
        var start = ( $scope.currentPage - 2 ) >= 1 ? $scope.currentPage - 2 : 1;//首页
        var end = ( $scope.currentPage + 2 ) <= $scope.totalPage ? $scope.currentPage + 2 : $scope.totalPage;//尾页

        $scope.frontdotShow = start > 1 ? true : false;//前面的省略号
        $scope.backdotShow = end < $scope.totalPage ? true : false;//后面的省略号
        for(var i = start; i <= end; i ++){
            $scope.pageList.push({
                number : i,
                active : i== $scope.currentPage ? 'active':''
            });//给点击的按钮添加class
        }
       // console.log(count);
    };
    
    
}
function cardHistoryController( $scope, $routeParams, $window, $location, $http, cardService ){

    $scope.app_id = $routeParams.app_id;
    $scope.card_id = $routeParams.card_id;
    $scope.history = function( ){

        cardService.history( $scope.card_id,$scope.app_id, function( json ){
            $scope.list = json.list;
            console.log($scope.list);

            for(var m = 0, i = $scope.list.length - 1; i >= 0; i--){
                if($scope.list[i].type == "pay"){
                    m += parseFloat($scope.list[i].fund);
                }
                if(m >= 120){
                    $scope.mark = i;
                    break;
                }
            }

        });
    };
    $scope.history();
}
function cardInfoController( $scope, $routeParams, $window, $location, $http, cardService ){

    $scope.app_id = $routeParams.app_id;
    $scope.transaction_no = $routeParams.transaction_no;
    $scope.information = function( ){

        cardService.information( $scope.transaction_no,$scope.app_id, function( json ){
            $scope.item = json;
            //console.log(json);
        });

    };

    $scope.information();
}

function cardTypeController( $scope, $routeParams, $window, $location, $http, cardService ){

    $scope.app_id = $routeParams.app_id;
    $scope.getList = function( ){

        cardService.typeList( $scope.app_id, function( json ){
            $scope.list = json.list;
        });

    };
    $scope.delType = function(item){
        cardService.delType( item,$scope.app_id, function( json ){
            $scope.getList();
        });
    };
    $scope.getList();
}

function cardTypeCreateController( $scope, $routeParams, $window, $location, $http, cardService ){

    $scope.app_id = $routeParams.app_id;
    $scope.code = $routeParams.code;
    if($scope.code != 0){
        cardService.find_type($scope.code, $scope.app_id, function( json ){
            $scope.type = json;
        } );
    }
    $scope.createType = function( type ){
        cardService.createType($scope.code, $scope.app_id, type, function( json ){

            $location.path( '/card/type' );

        } );

    };
}

function cardIndexController($scope, $routeParams, $window, $location, $http, cardService){
	//		var app_id = '5096fce5d7f8b112899f5bd7b2302bbe6e08d515';
//	    $scope.app_id = '5096fce5d7f8b112899f5bd7b2302bbe6e08d515';
   $scope.app_id = $routeParams.app_id;
    /*分页*/

    $scope.selectedPage = 1;
    $scope.data = {
        selectedPage  : $scope.selectedPage,
        page : $scope.page
    };
    $scope.limit = 20;
    $scope.getList = function( ){
        cardService.list( $scope.limit,$scope.selectedPage,$scope.app_id, function( json ){
//          $scope.list = json.list;
//         $scope.count =  json.count;
//          $scope.currentPage = 1 ;
//          $scope.getPage(json.count);
        });
    };
    $scope.searchCard = function(data ){
    	$scope.selectedPage = 1;
    	$scope.limit = 20;
        cardService.searchCard( data,$scope.limit, $scope.selectedPage,$scope.app_id, function( json ){
            $scope.list = json;
            $scope.count = json.count;
			$scope.currentPage = 1;
			$scope.getPage(json.count);
        });
    };
    $scope.getList();
    $scope.edit = function(){

    }
    
    /*分页*/
    $scope.changePage = function( p ){
        $scope.data.selectedPage = p;
        cardService.list( $scope.limit,p,$scope.app_id, function( json ){ 
            $scope.list = json.list;
           $scope.count =  json.count;
            $scope.currentPage = p ;
            $scope.getPage(json.count);
        });
    };
   $scope.getPage = function(count){
        $scope.pageList = [];
        var limit = 20;//返回的条数
        $scope.totalPage = parseInt(count / limit);//总页数
        $scope.totalPage += count % limit >= 1 ? 1 : 0;//判断页数是否是整数
        var start = ( $scope.currentPage - 2 ) >= 1 ? $scope.currentPage - 2 : 1;//首页
        var end = ( $scope.currentPage + 2 ) <= $scope.totalPage ? $scope.currentPage + 2 : $scope.totalPage;//尾页

        $scope.frontdotShow = start > 1 ? true : false;//前面的省略号
        $scope.backdotShow = end < $scope.totalPage ? true : false;//后面的省略号
        for(var i = start; i <= end; i ++){
            $scope.pageList.push({
                number : i,
                active : i== $scope.currentPage ? 'active':''
            });//给点击的按钮添加class
        }
       // console.log(count);
    };
    
    
}

